package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyTypeMappingMapper;
import com.geostar.constructionland.common.entity.analyse.AlyTypeMapping;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author chengan
 * @description
 */

@RestController
@Api(value = "分析映射表Ext接口")
@RequestMapping("/analyseMapped")
@Slf4j
public class AnalyseMappedController {
    @Autowired
    AlyTypeMappingMapper alyTypeMappingMapper;

    @PostMapping("/getAnalyseMapped")
    @ResponseBody
    @ApiOperation(value = "获取映射信息")
    ExtJsAjaxResponse getAnalyseMapped(@NotBlank(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
                                       @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
                                       String query_mapped_name) throws Exception {
        IPage<AlyTypeMapping> alyTypeMappingIPage = new Page<>(pageNo, pageNum);
        QueryWrapper<AlyTypeMapping> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(query_mapped_name)) {
            queryWrapper.like("ANALYSE_TYPE_MAPPED", query_mapped_name);
        }
        queryWrapper.orderByDesc("modifiedTime");
        alyTypeMappingIPage = alyTypeMappingMapper.selectPage(alyTypeMappingIPage, queryWrapper);
        ExtJsAjaxResponse extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(alyTypeMappingIPage);
        log.info("ExtJsResponse:{}", new ObjectMapper().writeValueAsString(extJsAjaxResponse));
        return extJsAjaxResponse;
    }

    @PostMapping("/insertAnalyseMapped")
    @ResponseBody
    @ApiOperation(value = "添加映射信息")
    ExtJsAjaxResponse addAnalyseMapped(@RequestBody AlyTypeMapping alyTypeMapping) {
        alyTypeMappingMapper.insert(alyTypeMapping);
        return ExtJsAjaxResponse.createBySuccessMessage("添加分析映射信息成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除映射信息")
    ExtJsAjaxResponse deleteAnalyseMapped(@NotBlank(message = "映射Id不能为空") @RequestParam(value = "mappingId") String mappingId) {
        if (mappingId.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(mappingId.split(","));
            alyTypeMappingMapper.deleteBatchIds(versionIds);
        } else {
            alyTypeMappingMapper.deleteById(mappingId);
        }
        return ExtJsAjaxResponse.createBySuccessMessage("删除映射信息成功");
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新映射信息")
    ExtJsAjaxResponse updateAnalyseMapped(@RequestBody AlyTypeMapping alyTypeMapping) {
        alyTypeMappingMapper.updateById(alyTypeMapping);
        return ExtJsAjaxResponse.createBySuccessMessage("更新映射信息成功");
    }

}
